package com.baiyi.homeui.hshomeui.util;

import android.util.Log;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileUtil {
    //往节点写值，pach为要写的文件节点，value为要写入的值
    public static void writeData(String path, String value) {
        try {
            BufferedWriter bufWriter = null;
            bufWriter = new BufferedWriter(new FileWriter(path));
            bufWriter.write(value + "");
            bufWriter.close();
            Log.d("FileUtil", "write writeData: " + value);
        } catch (IOException e) {
            Log.e("FileUtil","erro= "+ Log.getStackTraceString(e));
        }
    }


    public static String readFile(String filename) {
        File file = new File(filename);
        if (!file.exists()) {
            return "";
        }
        FileReader fr = null;
        BufferedReader br = null;
        String str = null;
        try {
            fr = new FileReader(file);
            br = new BufferedReader(fr);
            str = br.readLine();
            if (str == null) {
                str = "";
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fr != null)
                    fr.close();
                if (br != null)
                    br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return str;
    }
}
